<template>
  <div style="height: 100%">
    <van-form ref="form">
      <van-field
        readonly
        clickable
        :value="formData.checkDate"
        label="核酸检测日期"
        required
        placeholder="点击选择核酸检测日期"
        @click="checkDateShow = true"
      />
      <van-calendar
        clearable
        v-model="checkDateShow"
        :min-date="minDate"
        :max-date="maxDate"
        :show-confirm="false"
        @confirm="onCheckDateConfirm"
      />
      <van-field
        readonly
        clickable
        name="核酸检测结果"
        is-link
        label="核酸检测结果"
        required
        :value="formData.checkResultLabel"
        placeholder="请选择核酸检测频次"
        @click="showCheckResultPicker = true"
      />
    </van-form>

    <!-- 核酸检测频次选择 -->
    <van-action-sheet
      v-model="showCheckResultPicker"
      cancel-text="取消"
      :actions="saNucleinChkResultCode"
      @select="onChkResultConfirm"
    />
    <full-loading v-if="loading"></full-loading>
  </div>
</template>
<script>
import {
  saveTestInfo,
  queryTestList,
} from "@/api/srvAssurance/epidprevention/riskArea/riskAreaPerson";
import { queryDic } from "@/api/common";
export default {
  data() {
    return {
      formData: {
        checkId: "",
        checkDate: "",
        checkResult: "",
        checkResultLabel: "",
        relaId: "",
        busiType: "4",
      },
      loading: false,
      fullscreenModal: false,
      showCheckResultPicker: false, // 核酸检测结果picker
      checkDateShow: false, //核酸检测日期show
      minDate: new Date(new Date().getFullYear() - 1, 0, 1),
      maxDate: new Date(new Date().getFullYear(), 11, 31),
      nucleinAcidData: [], //表格中的核酸检测数据
      saNucleinChkResultCode: [],
    };
  },
  mounted() {
    this.getDicData();
  },
  methods: {
    //日期格式
    formatDate(date) {
      return `${date.getFullYear()}-${
        date.getMonth() < 9
          ? "0" + (date.getMonth() + 1)
          : "" + (date.getMonth() + 1)
      }-${date.getDate() < 10 ? "0" + date.getDate() : "" + date.getDate()}`;
    },
    // 核酸检测时间选择
    onCheckDateConfirm(date) {
      this.formData.checkDate = this.formatDate(date);
      this.checkDateShow = false;
    },
    // 核酸检测结果选择
    onChkResultConfirm(item) {
      this.formData.checkResult = item.value;
      this.formData.checkResultLabel = item.text;
      this.showCheckResultPicker = false;
    },
    // 获取字典数据
    getDicData() {
      //获取核酸检测结果code
      queryDic("SA_NUCLEIN_CHK_RESULT_CODE").then((res) => {
        if (res.code == 1000) {
          this.saNucleinChkResultCode = res.data;
        }
      });
    },
    //
    onAdd() {
      this.fullscreenModal = true;
    },
    // 保存
    handleSave() {
      this.loading = true;
      this.$refs.form
        .validate()
        .then(async () => {
          let formData = { ...this.formData };
          //新增或修改基本信息
          this.loading = false;
          let res = await saveTestInfo(formData);
          if (res.code == "1000") {
            this.$Notify({ type: "success", message: "添加成功！" });
            var testStrs = { relaId: this.formData.relaId, busiType: "4" };
            let res = await queryTestList(testStrs);
            if (res.code == "1000" && res.data) {
            this.$emit("handleCheckRefresh", res.data);
            }
            this.$emit("close");
          } else {
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },
    //查询核酸检测信息
    async queryCheckList() {
      var testStrs = { relaId: this.formData.relaId, busiType: "4" };
      let res = await queryTestList(testStrs);
      if (res.code == "1000" && res.data) {
        this.nucleinAcidData = res.data;
      }
    },
    // 查询原本信息
    getData(obj) {
      this.isEdit = true;
      for (var key in this.formData) {
        if (obj[key] || obj[key] === 0) {
          this.formData[key] = obj[key];
        }
      }
    },
    // 重置表单
    resetForm() {
      this.formData = {
        checkId: "",
        checkDate: "",
        checkResult: "",
        checkResultLabel: "",
        relaId: "",
        busiType: "4",
      };
    },
  },
};
</script>

<style scoped lang="less">
.listCon {
  height: calc(100% - 240px);
  .cellContent {
    padding: 10px;

    .flex {
      display: flex;

      p:nth-of-type(1) {
        flex: inherit !important;
        width: 45%;
      }
    }
  }
}
</style>
